package com.example.e_book.Fragment;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.e_book.Activity.MainActivity;
import com.example.e_book.Activity.RegisterActivity;
import com.example.e_book.DAO.BookDAO;
import com.example.e_book.R;
import com.example.e_book.entity.Book;
import com.example.e_book.Adapter.BookAdapter;
import com.example.e_book.entity.User;

import java.util.ArrayList;
import java.util.List;

public class ClassifyFragment extends Fragment {

    private ListView lvCategories;
    private RecyclerView rvBooks;
    private BookAdapter bookAdapter;
    private BookDAO bookDAO;
    private List<Book> bookList = new ArrayList<>();
    private String[] categories = {"文学", "武侠", "历史", "玄幻", "科幻","悬疑","军事"};
    private User user;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_classify, container, false);

        if (getArguments() != null) {
            user = getArguments().getParcelable("USER");
        }

        lvCategories = view.findViewById(R.id.lv_categories);
        rvBooks = view.findViewById(R.id.rv_books);
        rvBooks.setLayoutManager(new LinearLayoutManager(getContext()));

        bookDAO = new BookDAO(getContext());

        // 初始化分类列表
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, categories);
        lvCategories.setAdapter(adapter);

        // 设置分类列表的点击事件
        lvCategories.setOnItemClickListener((parent, view1, position, id) -> {
            // 根据点击的分类加载书籍
            updateBooksList(categories[position]);
        });

        // 初始化书籍列表
        bookAdapter = new BookAdapter(bookList,getContext(),user);
        rvBooks.setAdapter(bookAdapter);

        return view;
    }

    @SuppressLint("Range")
    private void updateBooksList(String category) {
        try {
            bookList.clear();
            Cursor cursor = bookDAO.getBooksByCategory(category);
            if (cursor != null && cursor.moveToFirst()) {
                do {
                    Book book = new Book();
                    book.setBookId(cursor.getInt(cursor.getColumnIndex("book_id")));
                    book.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                    book.setAuthor(cursor.getString(cursor.getColumnIndex("author")));
                    book.setCategory(cursor.getString(cursor.getColumnIndex("category")));
                    book.setContentPath(cursor.getString(cursor.getColumnIndex("content_path")));
                    book.setCoverImagePath(cursor.getString(cursor.getColumnIndex("cover_image_path")));
                    bookList.add(book);
                    Log.d("ClassifyFragment", "Added book: " + book.getTitle()); // 调试日志
                } while (cursor.moveToNext());
                cursor.close();
            } else {
                Log.d("ClassifyFragment", "No books found for category: " + category); // 调试日志
            }
            bookAdapter.updateList(bookList);
            bookAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            Log.e("ClassifyFragment", "Error loading books", e); // 调试日志
        }
    }
}